package model.parser;

import java.util.*;

import model.semantics.Node;
import model.semantics.UserFunctionNode;

public class UserFunctionDefinition<T> extends FunctionDefinition<T> {
    protected List<String> argLabels;
    protected Node<T> myBody;

    public UserFunctionDefinition() {

    }

    public UserFunctionDefinition(Node<T> body, List<String> labels) {
        argLabels = labels;
        myBody = body;

        if (argLabels == null) {
            throw ParserException.BAD_ARGUMENTS;
        }
    }

    protected void setBody(Node<T> body) {
        myBody = body;
    }

    public Node<T> getBody() {
        return myBody;
    }

    @Override
    public Node<T> makeNewInstance(Deque<Node<T>> queue, boolean isPrefix) {
        if (argLabels == null) {
            // If we never assigned arg labels to the definition, the function
            // was never defined.
            throw ParserException.BAD_SYNTAX;
        }

        Node<T>[] args = getArgsFromQueue(queue, argLabels.size(), isPrefix);
        Map<String, Node<T>> argMap = new HashMap<String, Node<T>>();
        for (int i = 0; i < argLabels.size(); i++) {
            argMap.put(argLabels.get(i), args[i]);
        }

        return new UserFunctionNode<T>(this, argMap);
    }

}
